package com.sinai.mshab.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.libideas.validation.client.DefaultTextBoxSubject;
import com.google.gwt.libideas.validation.client.Subject;
import com.google.gwt.libideas.validation.client.Validator;
import com.google.gwt.libideas.validation.client.ValidatorController;
import com.google.gwt.libideas.validation.client.validator.USPhoneValidator;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.sinai.mshab.format.Direction;
import com.sinai.mshab.format.FormatPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Mshab implements EntryPoint {
	private TextBox box;
	private ValidatorController validator;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		FormatPanel panel = new FormatPanel();
		HorizontalPanel boxAndButton = new HorizontalPanel();
		box = new TextBox();
		Button checkValid = new Button("Validate");
		checkValid.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				validate();
			}
		});
		Subject textBoxSubject = new DefaultTextBoxSubject(box);
		Validator phoneValidator = new USPhoneValidator();
		validator = new ValidatorController(textBoxSubject, phoneValidator);

		boxAndButton.add(box);
		boxAndButton.add(checkValid);
		panel.add(boxAndButton);

		panel.addDecoration(boxAndButton, new Label("Hello"), Direction.TOP);

		RootPanel.get().add(panel);
	}

	public void validate() {

		validator.validate();
	}
}
